﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class create_service : BaseForm
    {
        public create_service()
            : base()
        {
        }

        public create_service(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbQueue.DataSource = Utility.queues(dbContext);
            cbQueue.DisplayMember = "Name";

            lbContracts.DataSource = Utility.contracts(dbContext);
            lbContracts.DisplayMember = "Name";

            cbAuthorization.DataSource = Utility.authorizations(dbContext);
            cbAuthorization.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            sb.AppendLine("CREATE SERVICE [" + tbName.Text + "]");

            if (chAuthorization.Checked)
            {
                sb.AppendLine("AUTHORIZATION " + cbAuthorization.Text + "");
            }

            sb.AppendLine("ON QUEUE " + cbQueue.Text + "");

            sb.AppendLine("(");

            int ile = 0;

            foreach (string item in lbAddedContracts.Items)
            {
                string p = "";

                if (ile == 0)
                {
                    p = "   ";
                }
                else
                {
                    p = "  ,";
                }

                if (item == "DEFAULT")
                {
                    sb.AppendLine(p + "[DEFAULT]");
                }
                else
                {
                    sb.AppendLine(p + "[" + item + "]");
                }

                ile++;
            }

            sb.AppendLine(")");

            tbCommand.Text = sb.ToString();
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void chAuthorization_CheckedChanged(object sender, EventArgs e)
        {
            cbAuthorization.Enabled = chAuthorization.Checked;

            Write();
        }

        private void cbAuthorization_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnRemoveInitiator_Click(object sender, EventArgs e)
        {
            if (lbAddedContracts.SelectedIndex > 0)
            {
                lbAddedContracts.Items.RemoveAt(lbAddedContracts.SelectedIndex);
            }

            Write();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lbContracts.SelectedIndex < 0)
            {
                return;
            }

            lbAddedContracts.Items.Add(lbContracts.Text);

            Write();
        }

        private void cbQueue_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

    }
}